
package com.be.beadmin.material.impl;

import com.be.beadmin.common.utils.JpaQueryBuilderNew;
import com.be.beadmin.material.entity.MaterialShortage;
import com.be.beadmin.material.entity.MaterialShortage_;
import cn.hutool.core.util.ObjectUtil;
import com.be.beadmin.common.entity.PageEntity;
import com.be.beadmin.common.exception.BeadminException;
import com.be.beadmin.common.utils.JpaQueryBuilder;
import com.be.beadmin.common.utils.JpaSortBuilder;
import lombok.RequiredArgsConstructor;
import com.be.beadmin.material.mapper.MaterialShortageMapperEx;
import com.be.beadmin.material.repository.MaterialShortageRepository;
import com.be.beadmin.material.service.MaterialShortageService;
import com.be.beadmin.material.dto.input.MaterialShortageDto;
import com.be.beadmin.material.dto.output.MaterialShortageEx;
import com.be.beadmin.material.dto.input.MaterialShortageQueryCriteria;
import com.be.beadmin.material.mapper.MaterialShortageMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Order;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
* @description 服务实现
* @author zmy
* @date 2023-07-17
**/
@Service
@RequiredArgsConstructor
public class MaterialShortageServiceImpl implements MaterialShortageService {

    @Autowired
    private  MaterialShortageRepository materialShortageRepository;
    @Autowired
    private  MaterialShortageMapper materialShortageMapper;
    @Autowired
    private  MaterialShortageMapperEx materialShortageExMapper;

    @Override
    public Page<MaterialShortageEx> getMaterialShortageByPage(PageEntity pageEntity) {
            Pageable pageable = PageRequest.of(
            pageEntity.getCurrent() -1,
            pageEntity.getPageSize());
            Page<MaterialShortage> page = materialShortageRepository.findAll((root, criteriaQuery, criteriaBuilder) -> {
                    List<Predicate> list = new ArrayList<>();
                    Predicate pre1 =   JpaQueryBuilderNew.getPredicate(root, pageEntity.getFilters(), criteriaBuilder);
                    list.add(pre1);
                    Predicate pre2 = criteriaBuilder.equal(root.get(MaterialShortage_.delete), 0); //过滤为0
                    list.add(pre2);
                    Predicate[] p = new Predicate[list.size()];
                    List<Order> orders = new ArrayList<>();
                    if(pageEntity.getSorters() != null && pageEntity.getSorters().size() > 0) {
                        orders = JpaSortBuilder.getPredicate(root, pageEntity.getSorters(), criteriaBuilder);
                        criteriaQuery.orderBy(orders);
                    } else {
                        criteriaQuery.orderBy(criteriaBuilder.desc(root.get(MaterialShortage_.createTime).as(Date.class)));
                    }
                    return  criteriaBuilder.and(list.toArray(p));
                    }
                    , pageable);
            Page<MaterialShortageEx> page1 = page.map(materialShortageExMapper::toEntity);
            return page1;

    }

    @Override
    public void addMaterialShortage(MaterialShortageDto materialShortageDto) {
            MaterialShortage result = materialShortageRepository.findMaterialShortageByIdAndDelete(materialShortageDto.getId(), 0);
            if(ObjectUtil.isNotEmpty(result)) {
                throw new BeadminException("MaterialShortage不存在,请重新查询");
            }
            MaterialShortage materialShortage = materialShortageMapper.toEntity(materialShortageDto);
            materialShortageRepository.save(materialShortage);
    }

    @Override
    public void updateMaterialShortage(MaterialShortageDto materialShortageDto) {
            MaterialShortage materialShortage = materialShortageRepository.findMaterialShortageById(materialShortageDto.getId());
            materialShortage = materialShortageMapper.toEntity(materialShortageDto);
            materialShortageRepository.save(materialShortage);
    }


    @Override
    public void delMaterialShortage(String id) {
        MaterialShortage materialShortage = materialShortageRepository.findMaterialShortageById(id);
        materialShortage.setDelete(1);
        materialShortageRepository.save(materialShortage);
    }

    @Override
    public void delMaterialShortages (List<String> ids) { }
}